package com.qianfeng.wuyang.himalaya.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.activity.MainActivity;
import com.qianfeng.wuyang.himalaya.activity.MyApplication;
import com.qianfeng.wuyang.himalaya.adapter.recommendadapter.FragmentRecommendAdapter;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.DiscoveryInfoEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.FocusImgInfoEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.GuesseEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.LikeEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.ListenInfoEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.ListenerEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.TopEntity;
import com.qianfeng.wuyang.himalaya.callback.RecommendFocusImgService;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * A simple {@link Fragment} subclass.
 */
public class RecommendFragment extends Fragment implements Callback<TopEntity> {
    private RecyclerView recyclerView;
    private MainActivity activity;

    private View rootView;
    private MyApplication application;
    //recyclerView adapter
    private FragmentRecommendAdapter recommendAdapter;

    private List<ListenerEntity> listenerList =new ArrayList<>();
    private List<ListenInfoEntity> listenInfoList = new ArrayList<>();

    //猜你喜欢
    private List<GuesseEntity> guessList = new ArrayList<>();
    private List<LikeEntity> likeEntityList = new ArrayList<>();

    //小编推荐
    private List<ListenInfoEntity> recommendList = new ArrayList<>();

    //发现新奇
    private List<DiscoveryInfoEntity> discoveryList=new ArrayList<>();

    //焦点图
    private List<FocusImgInfoEntity> focusimgInfoList = new ArrayList<>();
    private List<View> focuseViewList= new ArrayList<>();
    private List<View> tagImg = new ArrayList<>();//保存第一个图片以及最后一个图片


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView==null) {
            rootView = inflater.inflate(R.layout.fragment_recommend,container,false);

            activity= (MainActivity) getActivity();
            //总的  多套布局
            recyclerView = (RecyclerView) rootView.findViewById(R.id.recommend_recycler);
            recommendAdapter = new FragmentRecommendAdapter(getContext(),activity);
            recommendAdapter = new FragmentRecommendAdapter(getContext(), (MainActivity) getActivity());
            recyclerView.setAdapter(recommendAdapter);


        }

        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        application = (MyApplication) getActivity().getApplication();
        //初始化recyclerview 热门推荐
        initHotRecyclerview(application);

        //下载焦点视图
        application.getRecommendFocusImgService()
                .getFocusimgList("and-a1","android","true","true",2,"4.3.98")
                .enqueue(this);

        //下载第二个itemdiscovery
        RecommendFocusImgService service = new Retrofit.Builder().baseUrl("http://mobile.ximalaya.com")
                .addConverterFactory(GsonConverterFactory.create()).build().create(RecommendFocusImgService.class);
        service.getFocusimgList("and-a1","android","true","true",2,"4.3.98").enqueue(this);


    }

    //初始化热门推荐
    private void initHotRecyclerview(MyApplication application) {

        application.getRecommendService().getRecommendList("android").enqueue(this);

//        添加布局管理者
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);


    }


    @Override
    public void onResponse(Call<TopEntity> call, Response<TopEntity> response) {

        //热门推荐
        if (response.body().getHotRecommends()!=null) {
            if (listenerList.size()==0) {
//                listenInfoList.addAll(response.body().getHotRecommends().getList().get())
                listenerList.addAll(response.body().getHotRecommends().getList());
                recommendAdapter.addData(listenerList);
            }

        }
        //猜你喜欢
        if (response.body().getGuess()!=null) {
            GuesseEntity guesseEntity = new GuesseEntity();
            guesseEntity = response.body().getGuess();
            if (likeEntityList.size()==0) {
                guessList.add(guesseEntity);
                likeEntityList.addAll(response.body().getGuess().getList());
                recommendAdapter.addLike(guessList,likeEntityList);
            }

        }

        //小编推荐
        if (response.body().getEditorRecommendAlbums()!=null) {

            if (recommendList.size()==0) {
                recommendList.addAll(response.body().getEditorRecommendAlbums().getList());
                recommendAdapter.addRecommend(recommendList);
            }
        }

        // 头视图
        if (response.body().getFocusImages()!=null) {
//            &&focusimgInfoList.size()==0
            if (focusimgInfoList.size()==0) {
                ImageView imgone= new ImageView(getContext());
                ImageView imglast = new ImageView(getContext());
                focusimgInfoList.addAll(response.body().getFocusImages().getList());
                for (int i = 0; i < response.body().getFocusImages().getList().size(); i++) {
                    ImageView imageView= new ImageView(getContext());
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    if (response.body().getFocusImages().getList().get(i).getPic()!=null) {
                        Picasso.with(getContext())
                                .load(response.body().getFocusImages().getList().get(i).getPic())
                                .into(imageView);
                        if (i==0&&tagImg.size()==0) {
                            //第一张图片
                            Picasso.with(getContext())
                                    .load(response.body().getFocusImages().getList().get(i).getPic())
                                    .into(imgone);

                        }
                        focuseViewList.add(imageView);
                        if (i==response.body().getFocusImages().getList().size()-1){
                            Picasso.with(getContext())
                                    .load(response.body().getFocusImages().getList().get(i).getPic())
                                    .into(imglast);
                        }
                    }
                }
                if (focuseViewList.size()!=0) {
                    focuseViewList.add(0,imglast);
                    focuseViewList.add(response.body().getFocusImages().getList().size()+1,imgone);
                }

                recommendAdapter.addFocusData(focuseViewList,focusimgInfoList);
            }
        }
        //discovery    &&discoveryList==null??不OK？？  list 只要new 它就不为null ；size
        if (response.body().getDiscoveryColumns()!=null&&discoveryList.size()==0){
            discoveryList.addAll(response.body().getDiscoveryColumns().getList());
            recommendAdapter.addDiscovery(discoveryList);
        }

    }

    @Override
    public void onFailure(Call<TopEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
    }

}
